package cn.cxw.baidu;

import android.app.Activity;
import android.graphics.Point;
import android.graphics.Rect;

import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.MapStatus;
import com.baidu.mapapi.map.MapStatusUpdate;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.model.LatLng;

import cn.cxw.core.Constants;
import cn.cxw.model.GaussPoint;
import cn.cxw.model.MapRect;
import cn.cxw.util.CoordinateUtil;

/**
 * Created by CXW-IBM on 2017/9/3.
 */

public class MapViewHelper {
    public static Rect windowRect;
    public static int statusBarHeight = -1;

    /**
     * 设置中心点
     */
    public static void setUserMapCenter(BaiduMap mBaiduMap, GaussPoint p) {
        //将高斯坐标转换为经纬度
        LatLng cenpt = CoordinateUtil.GaussToGPS(p.x, p.y);
        float zoom = Constants.DEFAULT_LEVEL;
        cenpt = CoordinateUtil.GPSToBD(cenpt.latitude, cenpt.longitude);
        setUserMapCenter(mBaiduMap, cenpt, zoom);
    }

    /**
     * 设置中心点
     */
    public static void setUserMapCenter(BaiduMap mBaiduMap, LatLng pt) {
        //将高斯坐标转换为经纬度
        float zoom = Constants.DEFAULT_LEVEL;
        setUserMapCenter(mBaiduMap, pt, zoom);
    }

    public static void setUserMapCenter(BaiduMap mBaiduMap, LatLng cenpt, float zoom) {
        //定义地图状态
        MapStatus mMapStatus = new MapStatus.Builder()
                .target(cenpt)
                .zoom(zoom)
                .build();
        //定义MapStatusUpdate对象，以便描述地图状态将要发生的变化
        MapStatusUpdate mMapStatusUpdate = MapStatusUpdateFactory.newMapStatus(mMapStatus);
        //动画切换改变地图状态
        //mBaiduMap.setMapStatus(mMapStatusUpdate);
        mBaiduMap.animateMapStatus(mMapStatusUpdate);
    }

    //得到地图4个角的点坐标
    public static MapRect getMapRect(Activity activity, BaiduMap mBaiduMap){
        MapRect mr = new MapRect();

        //左上角
        Point pNW = new Point();
        pNW.x = 0;
        pNW.y = getStatusBarHeight(activity);
        LatLng llNW = mBaiduMap.getProjection().fromScreenLocation(pNW);
        mr.nw = new cn.cxw.model.Point(llNW);

        //左下角
        Point pSW = new Point();
        pSW.x = 0;
        pSW.y = getStatusBarHeight(activity) + getWindowRect(activity).height();
        LatLng llSW = mBaiduMap.getProjection().fromScreenLocation(pSW);
        mr.sw = new cn.cxw.model.Point(llSW);

        //右上角
        Point pNE = new Point();
        pNE.x = getWindowRect(activity).width();
        pNE.y = getStatusBarHeight(activity);
        LatLng llNE = mBaiduMap.getProjection().fromScreenLocation(pNE);
        mr.ne = new cn.cxw.model.Point(llNE);

        //左下角
        Point pSE = new Point();
        pSE.x = getWindowRect(activity).width();
        pSE.y = getStatusBarHeight(activity) + getWindowRect(activity).height();
        LatLng llSE = mBaiduMap.getProjection().fromScreenLocation(pSE);
        mr.se = new cn.cxw.model.Point(llSE);

        return mr;
    }

    //得到窗体区域
    public static Rect getWindowRect(Activity activity){
        if(windowRect != null){
            return windowRect;
        }

        windowRect = new Rect();
        activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(windowRect);
        return windowRect;
    }

    //获取状态栏高度
    public static int getStatusBarHeight(Activity activity){
        if(statusBarHeight >= 0){
            return statusBarHeight;
        }

        //获取status_bar_height资源的ID
        int resourceId = activity.getResources().getIdentifier("status_bar_height", "dimen", "android");
        if (resourceId > 0) {
            //根据资源ID获取响应的尺寸值
            statusBarHeight = activity.getResources().getDimensionPixelSize(resourceId);
        }
        return statusBarHeight;
    }
}
